<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="formData.name" placeholder="品牌名称" clearable />
                </el-form-item>
                <el-form-item label="图标" prop="logo">
                    <div>
                        <div>
                            <material-picker v-model="formData.logo" :limit="1" />
                        </div>
                        <div class="form-tips">建议尺寸：100*100px，支持jpg，jpeg，png格式</div>
                    </div>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="formData.sort" placeholder="请输入名称" clearable />
                </el-form-item>
                <el-form-item label="描述" prop="description">
                    <el-input
                        v-model="formData.description"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6 }"
                        placeholder="请输入备注"
                        maxlength="200"
                        show-word-limit
                    />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import { add1, edit1, detail2 } from '@/servers/goods/api/supplyChainshangpinpinpai'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
let id: any = undefined
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑品牌' : '新建品牌'
})

const formData = reactive({
    description: undefined,
    sort: undefined,
    logo: undefined,
    name: ''
})

const formRules = reactive({
    name: [
        {
            required: true,
            message: '请输入品牌名称',
            trigger: ['blur']
        }
    ]
})

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit'
        ? await edit1(
              {
                  id
              },
              formData
          )
        : await add1(formData)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add', rowId: any) => {
    mode.value = type
    id = rowId
    popupRef.value?.open()
}

const setFormData = async (row: any) => {
    const data = await detail2({
        id: row.id
    })
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData
})
</script>
